
package com.axxc.cloud.config.exception;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
public class GlobalExceptionHandler {

	@ExceptionHandler(Exception.class)
	public ResponseEntity<ResponseExeceptionVO> defauleErrorHandler(HttpServletRequest request, Exception e) {
		ResponseExeceptionVO responseVO = new ResponseExeceptionVO();
		responseVO.setMessage(e.getMessage());
		responseVO.setClientIP(request.getRemoteAddr());
		responseVO.setCurrentTime(new Date());
		responseVO.setHttpMethod(request.getMethod());
		responseVO.setHttpProtocol(request.getProtocol());
		responseVO.setRequestURL(request.getRequestURL().toString());
		responseVO.setServerIP(request.getLocalAddr());
		responseVO.setRequestTime(
				new Date(Long.valueOf(request.getParameter("t") == null ? "0" : request.getParameter("t"))));
		if (e instanceof NoHandlerFoundException) {
			responseVO.setStatus(HttpStatus.NOT_FOUND);
		} else {
			responseVO.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return new ResponseEntity<ResponseExeceptionVO>(responseVO, HttpStatus.OK);
	}
}
